package com.weiwei.diancan.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.weiwei.diancan.R;
import com.weiwei.diancan.bean.LoginBean;
import com.weiwei.diancan.bean.TabBean;

import java.util.ArrayList;
import java.util.List;


/**
 * 右侧主界面ListView的适配器
 * 
 * @author Administrator
 * 
 */
public class HomeAdapter extends BaseAdapter {
	private Context context;
	private List<LoginBean.TypeListBean> foodDatas;
	private List<TabBean> tabBeen;
	public HomeAdapter(Context context, List<LoginBean.TypeListBean> foodDatas) {
		this.context = context;
		this.foodDatas = foodDatas;
		setData(foodDatas);
	}

	public void setData(List<LoginBean.TypeListBean> foodDatas){
		tabBeen = new ArrayList<>();
		for(int i = 0;i<foodDatas.size();i++){
			List<LoginBean.TypeListBean.TabListBean> tab_list = foodDatas.get(i).getTab_list();
			for(int j = 0;j<tab_list.size();j++){
				tabBeen.add(new TabBean(tab_list.get(j),foodDatas.get(i).getType_id(),foodDatas.get(i).getType_name()));
			}
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(tabBeen!=null){
			return tabBeen.size();
		}else {		
			return 10;
		}		
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHold holder = null;
		if(arg1 == null){
			arg1 = View.inflate(context, R.layout.item_main_detail_child, null);
			holder = new ViewHold();
			holder.tv_title = (TextView)arg1.findViewById(R.id.tv_fenlei);
			holder.tv_name = (TextView)arg1.findViewById(R.id.tv_table_name);
			holder.tv_people_num = (TextView)arg1.findViewById(R.id.tv_people_num);
			holder.tv_null = (TextView)arg1.findViewById(R.id.tv_null);
			holder.tv_price = (TextView)arg1.findViewById(R.id.tv_price);
			arg1.setTag(holder);
		}else {
			holder = (ViewHold)arg1.getTag();
		}
		holder.tv_name.setText(tabBeen.get(arg0).getTabListBean().getTab_name());
		holder.tv_title.setText(tabBeen.get(arg0).getTitle());
		if(arg0 == 0){
			holder.tv_title.setVisibility(View.VISIBLE);
		}else if (!TextUtils.equals(tabBeen.get(arg0).getTitle(),tabBeen.get(arg0 -1).getTitle())) {
			holder.tv_title.setVisibility(View.VISIBLE);
		}else {
			holder.tv_title.setVisibility(View.GONE);
		}

		LoginBean.TypeListBean.TabListBean tabListBean = tabBeen.get(arg0).getTabListBean();
		if(tabListBean.getTab_state().equals("1")||tabListBean.getTab_state().equals("0")){
			holder.tv_null.setVisibility(View.VISIBLE);

			if(tabListBean.getTab_state().equals("1")){
				holder.tv_null.setText("空闲");
				holder.tv_null.setBackgroundResource(R.drawable.kongxian_shape);
				holder.tv_null.setTextColor(Color.parseColor("#000000"));
			}else{
				holder.tv_null.setText("暂停");
				holder.tv_null.setBackgroundResource(R.drawable.zanting_shape);
				holder.tv_null.setTextColor(Color.parseColor("#B41B1B"));
			}

			holder.tv_price.setVisibility(View.GONE);
			holder.tv_name.setTextColor(Color.parseColor("#181818"));
			holder.tv_people_num.setText("0/"+tabListBean.getTab_person());
			holder.tv_people_num.setTextColor(Color.parseColor("#181818"));
		}else {
			holder.tv_null.setVisibility(View.GONE);
			holder.tv_price.setVisibility(View.VISIBLE);
			holder.tv_price.setText("¥"+tabListBean.getTab_price());
			holder.tv_name.setTextColor(Color.parseColor("#B41B1B"));

			if(tabListBean.getOrder_list()==null||tabListBean.getOrder_list().size()==0){
				holder.tv_people_num.setText("0/"+tabListBean.getTab_person());
			}else {
				holder.tv_people_num.setText(tabListBean.getOrder_list().get(0).getOrder_person() + "/" + tabListBean.getTab_person());
				List<LoginBean.OrderListBean> order_list = tabListBean.getOrder_list();
				double  privces = 0;
				for(LoginBean.OrderListBean orderListBean:order_list){
					List<LoginBean.OrderListBean.LogListBean> log_list = orderListBean.getLog_list();
					if(log_list!=null){
						for(LoginBean.OrderListBean.LogListBean logListBean :log_list){
							String log_money = logListBean.getLog_money();
							privces+=Double.parseDouble(log_money);
						}
					}
				}
				holder.tv_price.setText("¥"+privces);
			}
			holder.tv_people_num.setTextColor(Color.parseColor("#B41B1B"));
		}

		return arg1;
	}
	private static class ViewHold{
		private TextView tv_title;
		private TextView tv_name;
		private TextView tv_people_num;
		private TextView tv_null;
		private TextView tv_price;
	}
}
